/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ComExWeb.ui.beans;

import ComExWeb.dominio.TipoEndereco;
import ComExWeb.dominio.Endereco;
import ComExWeb.util.JsfUtil;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;
import java.net.Proxy.Type;
import javax.faces.event.ValueChangeEvent;
import org.alfredlibrary.ProxyConfig;
import org.alfredlibrary.utilitarios.correios.CEP;
import org.alfredlibrary.utilitarios.net.WorldWideWeb;
import org.alfredlibrary.validadores.CPF;
import org.primefaces.component.panel.Panel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author 002824992500
 */
@ManagedBean
@ViewScoped
public class EnderecoJSFBean extends BaseCRUDJSFBean {

    private static final Logger log = LoggerFactory.getLogger(EnderecoJSFBean.class);
    //private static final Logger log = Logger.getLogger(EnderecoJSFBean.class.getName());

    public Endereco getEndereco() {
        return getObjSelecionado();
    }

    public void setEndereco(Endereco endereco) {
        setObjSelecionado(endereco);
    }
    private List<Endereco> enderecos = new ArrayList<Endereco>();

    public List<Endereco> getEnderecos() {
        return enderecos;
    }
    private List<TipoEndereco> tipoEnderecos = new ArrayList<TipoEndereco>();

    public List<TipoEndereco> getTipoEnderecos() {
        return tipoEnderecos;
    }

    /** Creates a new instance of ColaboradorJSFBean */
    public EnderecoJSFBean() {
    }

    public void buscaCep(ValueChangeEvent vce) {

        ProxyConfig prox = new ProxyConfig("10.1.1.165", 3128, Type.HTTP);
        WorldWideWeb.setProxyConfig(prox);
        String[] endereco = null;

        try {
            //pesquisa no site CEP livre
            endereco = CEP.consultarEnderecoCEPLivre((String) vce.getNewValue());
            //pesquisa no site dos correios
            //String[] endereco = CEP.consultarEnderecoCorreios((String) vce.getNewValue());
            //System.out.println(endereco[4].toString());
            getEndereco().setDsLogradouro(endereco[0].toString() + " " + endereco[1].toString());
            getEndereco().setDsBairro(endereco[2].toString());
            getEndereco().setDsCidade(endereco[3].toString());
            getEndereco().setDsEstado(endereco[4].toString());

        } catch (Exception e) {
            error("CEP não encontrado.");
            setEndereco(null);

        }




        //setSongDetail((String)detail.get(songvalue));
        //paneilEndereco.setOnToggleUpdate(null);
        //this.setTesteLogradouro("Rua virgilio vidigal");
        //  FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(testeLogradouro, vce.getNewValue());
        //FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("name","Rua virgilio" );
                /*Render the response*/ // FacesContext.getCurrentInstance().renderResponse();
    }

    @Override
    public void setup() {
        try {
            renovarTabela();
            this.tipoEnderecos = getCrudService().findAll(TipoEndereco.class);

            setPaginaForm("form_endereco");
            setPaginaListagem("list_endereco");
            setCrudClass(Endereco.class);
        } catch (Exception e) {
            String err = "Erro na inicialização: " + e.getMessage();
            error(err);
            //log.error(err, e);
        }
    }

    @Override
    protected void renovarTabela() {
        this.enderecos = getCrudService().findAll(Endereco.class);
    }
}
